﻿using Quartz;

namespace TrafficWebApi.Services
{
    /// <summary>
    /// 定时任务类
    /// </summary>
    public class JobScheduler : IHostedService
    {
        #region PageCls
        private readonly ISchedulerFactory _schedulerFactory;

        /// <summary>
        /// 计划
        /// </summary>
        public IScheduler _scheduler { get; set; }

        public JobScheduler(ISchedulerFactory schedulerFactory) => _schedulerFactory = schedulerFactory;
        #endregion

        /// <summary>
        /// 定时服务任务开启注册
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _scheduler = await _schedulerFactory.GetScheduler(cancellationToken);

            await _scheduler.Start(cancellationToken);
        }

        /// <summary>
        /// 定时服务任务停止
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            _scheduler = await _schedulerFactory.GetScheduler(cancellationToken);
            await _scheduler.Shutdown(cancellationToken);
        }
    }
}
